package ie.nolantech.gpsg;

import java.util.ArrayList;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity {

	static final String TAG = "MapActivity";

	private MapController mController;
	private MyLocationOverlay myLoc;
	private Cursor c;
	private GpsHelper mHelper;
	private HelloItemizedOverlay mOverlay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

	}

	@Override
	protected void onResume() {
		MapView map = (MapView) findViewById(R.id.map);

		mHelper = new GpsHelper(this);
		mController = map.getController();

		mOverlay = new HelloItemizedOverlay(this.getResources().getDrawable(
				R.drawable.pin));

		Intent i = getIntent();
		Bundle ex = i.getExtras();
		if (ex != null) {
			String lat = (String) ex.get("lat");
			if (lat != null) {
				String lon = (String) i.getExtras().get("lon");
				GeoPoint forDisplay = new GeoPoint(
						(int) (Float.parseFloat(lat) * 1e6),
						(int) (Float.parseFloat(lon) * 1e6));
				OverlayItem item = new OverlayItem(forDisplay, "", "");

				mOverlay.addOverlay(item);
				map.getOverlays().add(mOverlay);
				mController.setCenter(forDisplay);
			}
		} else {
			showOverlay(map);
		}

		super.onResume();
	}

	private void showOverlay(MapView map) {

		c = mHelper.getAllLoc(10);
		if (c.moveToFirst()) {
			int index = 0;
			do {
				Log.i(TAG, "index:" + index++);
				int lat = c.getInt(c.getColumnIndex("lat"));
				int lon = c.getInt(c.getColumnIndex("lon"));
				String dateDone = c.getString(c.getColumnIndex("date_done"));
				OverlayItem item = new OverlayItem(new GeoPoint(
						(int) (lat * 1e6), (int) (lon * 1e6)), dateDone,
						dateDone);

				mOverlay.addOverlay(item);

			} while (c.moveToNext());
		} else {
			Log.d(TAG, "There were no values in the cursor.");
		}

		map.getOverlays().add(mOverlay);

		// myLoc = new MyLocationOverlay(this, map);
		// myLoc.enableMyLocation();
		// map.getOverlays().add(myLoc);

		map.setBuiltInZoomControls(true);

		// GeoPoint here = myLoc.getMyLocation();
		// if (here != null) {
		// mController.setCenter(myLoc.getMyLocation());
		// }

		mController
				.zoomToSpan(mOverlay.getLatSpanE6(), mOverlay.getLonSpanE6());
	}

	protected void onPause() {
		if (myLoc != null) {
			myLoc.disableMyLocation();
		}
		if (c != null) {
			c.close();
		}

		super.onPause();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
		private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

		public HelloItemizedOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
		}

		public void addOverlay(OverlayItem item) {
			mOverlays.add(item);
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return mOverlays.get(i);
		}

		@Override
		public int size() {
			return mOverlays.size();
		}

		@Override
		public boolean onTap(GeoPoint p, MapView mapView) {
			// controller.zoomIn();
			mController.animateTo(p);
			return super.onTap(p, mapView);
		}

	}
}
